package com.example.appointment.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.example.appointment.AppointmentApplication;
import com.example.appointment.meta.QQLoginVO;
import com.example.appointment.meta.QQUserVO;
import com.google.gson.Gson;
import com.tencent.connect.UserInfo;
import com.tencent.open.utils.HttpUtils;
import com.tencent.tauth.IRequestListener;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;

/**
 * Created by Admin on 2016/9/14.
 */
public abstract class BaseLoginActivity extends Activity {

    protected Tencent mTencent;
    protected IRequestListener mListener;

    public QQLoginVO qqLoginVO;
    private UserInfo userInfo;

    public void initTencent() {
        qqLoginVO = AppointmentApplication.getInstance().getQQLogin();

        if (!TextUtils.isEmpty(qqLoginVO.getOpenid())) {
            mTencent.setOpenId(qqLoginVO.getOpenid());
        }

        if (!TextUtils.isEmpty(qqLoginVO.getAccess_token())) {
            mTencent.setAccessToken(qqLoginVO.getAccess_token(), qqLoginVO.getExpires_in());
        }

        //校验登陆是否有效
        if (!mTencent.isSessionValid()) {
            login();
        } else {
            startActivity(new Intent(this, HomePageActivity.class));
        }
    }

    public void login() {
        mTencent.login(this, "all", new IUiListener() {
            @Override
            public void onComplete(Object o) {
                QQLoginVO qqLoginVO = new Gson().fromJson(o.toString(), QQLoginVO.class);
                showMessage("Login Success" + qqLoginVO.toString());
                //保存登陆信息
                AppointmentApplication.getInstance().setQQLogin(qqLoginVO);
                //获取QQ用户信息
//                getUserInfo();
            }

            @Override
            public void onError(UiError uiError) {
                showMessage("Login Error" + uiError.errorMessage);
            }

            @Override
            public void onCancel() {
                showMessage("Login Cancel");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mTencent != null) {
            mTencent.onActivityResult(requestCode, resultCode, data);
        }
    }

    protected abstract void showMessage(String s);

    //获取QQ用户登陆信息
    public void getUserInfo() {
        userInfo = new UserInfo(getApplicationContext(), mTencent.getQQToken());
        userInfo.getUserInfo(new IUiListener() {
            @Override
            public void onComplete(Object o) {
                QQUserVO qqUserVO = new Gson().fromJson(o.toString(), QQUserVO.class);
                AppointmentApplication.getInstance().setQQUserVO(qqUserVO);
                showMessage("用户信息" + o.toString());
            }

            @Override
            public void onError(UiError uiError) {
                showMessage("获取用户信息失败" + uiError.errorMessage + uiError.errorDetail + uiError.errorCode);
            }

            @Override
            public void onCancel() {
                showMessage("取消");
            }
        });
    }

    public void getUserInfoWithListener() {

        mListener = new IRequestListener() {
            @Override
            public void onComplete(JSONObject jsonObject) {
                showMessage("异步说明" + jsonObject.toString());
            }

            @Override
            public void onIOException(IOException e) {
                showMessage("onIOException" + e.getMessage());
            }

            @Override
            public void onMalformedURLException(MalformedURLException e) {
                showMessage("MalformedURLException" + e.getMessage());
            }

            @Override
            public void onJSONException(JSONException e) {
                showMessage("JSONException" + e.getMessage());
            }

            @Override
            public void onConnectTimeoutException(ConnectTimeoutException e) {
                showMessage("ConnectTimeoutException" + e.getMessage());
            }

            @Override
            public void onSocketTimeoutException(SocketTimeoutException e) {
                showMessage("SocketTimeoutException" + e.getMessage());
            }

            @Override
            public void onNetworkUnavailableException(HttpUtils.NetworkUnavailableException e) {
                showMessage("NetworkUnavailableException" + e.getMessage());
            }

            @Override
            public void onHttpStatusException(HttpUtils.HttpStatusException e) {
                showMessage("HttpStatusException" + e.getMessage());
            }

            @Override
            public void onUnknowException(Exception e) {
                showMessage("Exception" + e.getMessage());
            }

        };
    }

}


